const axios = require("axios");
const SocksProxyAgent = require("socks-proxy-agent");
const cheerio = require("cheerio");
const md5 = require("md5");
const { promisify } = require("util");
const path = require("path");
const fs = require("fs");
const { resolve, format } = require("path");
const { exec } = require("child_process");
const { rejects } = require("assert");
const writeFileAsync = promisify(fs.writeFile);
const execAsync = promisify(exec);
const statAsync = promisify(fs.stat);

const ua =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36";
const socksPort = 7891;

async function wait(time = 5) {
    console.log(`等待,${time}`);
    return new Promise((resolve) => {
        setTimeout(resolve, 1000 * time);
    });
}

async function get(url, options = {}, config = {}) {
    const { socks } = config;
    await wait();
    const source = axios.CancelToken.source();
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            source.cancel("请求超时.....");
            reject();
        }, 10000);
        axios
            .get(url, {
                httpsAgent: socks
                    ? new SocksProxyAgent(`socks://127.0.0.1:${socksPort}`)
                    : undefined,
                headers: { "user-agent": ua },
                timeout: 10000,
                cancelToken: source.token,
                ...options,
            })
            .then(resolve)
            .catch(reject);
    });
}

async function getImage(url) {
    return await get(url, { responseType: "arraybuffer" });
}

function jquery(html) {
    return cheerio.load(html, { decodeEntities: false });
}

(async () => {
    try {
        for (let i = 1; i < 2; i++) {
            try {
                const html = (await get(`https://bbs.hupu.com/selfie-${i}`))
                    .data;
                const $ = jquery(html);
                const list = $("html").find(".for-list >li");
                for (let j = 0; j < list.length; j++) {
                    try {
                        const name = $(list[j]).find("a").text();
                        const link = $(list[j]).find("a").attr("href");
                        console.log(name, link);
                        if (name && link) {
                            const detail = (
                                await get(`https://bbs.hupu.com${link}`)
                            ).data;
                            const $1 = jquery(detail);
                            const imgs = $1("html").find("p > img");
                            console.log("图片长度：", imgs.length);
                            if (imgs.length) {
                                await execAsync(
                                    `cd imgs && mkdir ${md5(name)}`
                                ).catch(() => {});
                            }
                            for (let k = 0; k < imgs.length; k++) {
                                try {
                                    const imgLink = $1(imgs[k]).attr("src");
                                    if (imgLink) {
                                        const imgData = (
                                            await getImage(imgLink)
                                        ).data;
                                        await writeFileAsync(
                                            path.resolve(
                                                __dirname,
                                                `./imgs/${md5(name)}/${md5(
                                                    imgLink
                                                )}.jpg`
                                            ),
                                            imgData
                                        );
                                        console.log("创建成功");
                                    }
                                } catch (e) {
                                    console.log(e);
                                }
                            }
                        }
                    } catch (e) {
                        console.log(e);
                    }
                }
            } catch (e) {
                console.log(e);
            }
        }
    } catch (e) {
        console.log(e);
    }
})();
